/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.query;

import com.inspur.edp.bef.api.lcp.AuthInfo;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.spi.event.query.BffQueryEventBroker;
import com.inspur.edp.bff.spi.utils.AuthUtils;
import com.inspur.edp.cef.entity.condition.EntityFilter;

public class QueryWithAuthInfoAction extends QueryAction {
    private AuthInfo authorityInfo;

    public QueryWithAuthInfoAction(EntityFilter filter, AuthInfo authorityInfo) {
        super(filter);
        this.authorityInfo = authorityInfo;
    }

    @Override
    protected void setQueryContext(QueryContext queryContext) {
        super.setQueryContext(queryContext);
        if (authorityInfo != null && authorityInfo.getExtType() != null && "".equals(authorityInfo.getExtType()) == false) {
            queryContext.setUseCustomAuth(true);
            queryContext.setAuthInfo(new AuthInfo());
            AuthUtils.buildAuthInfo(
                    queryContext.getAuthInfo(),
                    authorityInfo.getExtType(),
                    authorityInfo.getExtend1(),
                    authorityInfo.getExtend2(),
                    authorityInfo.getExtend3(),
                    authorityInfo.getExtend4(),
                    authorityInfo.getExtend5()
            );
            BffQueryEventBroker.fireprocessGetAuth(authorityInfo);
        }
    }

    @Override
    protected IStandardLcp getQueryLcp() {
        IStandardLcp lcp = super.getQueryLcp();
        if (authorityInfo != null && authorityInfo.getExtType() != null && "".equals(authorityInfo.getExtType()) == false) {
            AuthUtils.buildAuthInfo(
                    lcp.getBefContext().getAuthInfo(),
                    authorityInfo.getExtType(),
                    authorityInfo.getExtend1(),
                    authorityInfo.getExtend2(),
                    authorityInfo.getExtend3(),
                    authorityInfo.getExtend4(),
                    authorityInfo.getExtend5()
            );
            BffQueryEventBroker.fireprocessGetAuth(authorityInfo);
        }
        return lcp;
    }
}
